package com.jlhlyby.concurrence;

/**
 * 两个线程交叉打数
 * 一个打单数一个打偶数
 */
public class TwoThreadPrintNum {
    public static void main(String[] args) {
        new PrintThread().start();
        new PrintThread().start();
    }

    static class PrintThread extends Thread {
        private static volatile int num = 0;
        private static final int max = 1000;
        private static final String lock = "";
        @Override
        public void run() {
            super.run();
            synchronized (lock) {
                while (max >= num) {
                    System.out.println(getName() + " : " + num++);
                    lock.notify();//唤醒wait中的线程去竞争锁资源
                    try {
                        lock.wait();//进入阻塞状态并且释放锁资源
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }


        }
    }
}

